$(".editGroupBtn").click(function () {
    group_id = $(this).parent().parent().children(0).html();
    $.get("/group/getbyid", {GroupID: group_id}, function (data) {
        var jsonStr = JSON.stringify(data);
        var jsonObj = JSON.parse(jsonStr);

        $("#editGroupName").val(jsonObj.groupName);
        $("#IsAnalysis").prop("checked",false);
        $("#IsUpload").prop("checked",false);
        $('#IsThreshold').prop("checked",false);
        $("#IsAlarm").prop("checked",false);
        $("#IsNotice").prop("checked",false);


        if (jsonObj.isAnalysis == 1) {
            $("#IsAnalysis").attr("checked", 'checked');
        }
        if (jsonObj.isUpload == 1) {
            $("#IsUpload").attr("checked", 'checked');
        }
        if (jsonObj.isThreshold == 1) {
            $("#IsThreshold").attr("checked", 'checked');
        }
        if (jsonObj.isAlarm == 1) {
            $("#IsAlarm").attr("checked", 'checked');
        }
        if (jsonObj.isNotice == 1) {
            $("#IsNotice").attr("checked", 'checked');
        }


    });
});

var delUri;
var group_id;
$(".delGroupBtn").click(function () {
    delUri = $(this).attr("del_uri");
});

$("#delConfirm").click(function () {

    $("#deleteGroupForm").attr("action", delUri).submit();

});

$("#addSubmit").click(function () {
    var isAlarm = 0;
    var isAnalysis = 0;
    var isNotice = 0;
    var isThreshold = 0;
    var isUpload = 0 ;

    if($('input[name="IsAlarm"]').is(':checked')){
        isAlarm = 1;
    }if($('input[name="IsAnalysis"]').is(':checked')){
        isAnalysis = 1;
    }if($('input[name="IsNotice"]').is(':checked')){
        isNotice = 1;
    }if($('input[name="IsThreshold"]').is(':checked')){
        isThreshold = 1;
    }if($('input[name="IsUpload"]').is(':checked')){
        isUpload = 1;
    }

    var domainId = $("#addGroupSelect option:selected").val();
    var domainName = $('#addGroupSelect option:selected').html();
    var groupName = $('#GroupInputName').val();

    $.post('/group/add',{GroupID:group_id,groupName:groupName,
        DomainName:domainName,DomainID:domainId,IsAnalysis:isAnalysis,
        IsUpload:isUpload,IsThreshold:isThreshold,IsAlarm:isAlarm,IsNotice:isNotice});
    window.location.reload();
});

$("#groupUpdate").click(function () {
    var isAlarm = 0;
    var isAnalysis = 0;
    var isNotice = 0;
    var isThreshold = 0;
    var isUpload = 0 ;
    if($('#IsAlarm').is(':checked')){
        isAlarm = 1;
    }if($('#IsAnalysis').is(':checked')){
        isAnalysis = 1;
    }if($('#IsNotice').is(':checked')) {
        isNotice = 1;
    }if($('#IsThreshold').is(':checked')) {
        isThreshold = 1;
    }if($('#IsUpload').is(':checked')) {
        isUpload = 1;
    }
    var domainId = $("#selectGroup option:selected").val();
    var domainName = $('#selectGroup option:selected').html();
    var groupName = $('#editGroupName').val();

    $.post('/group/updategroup',{GroupID:group_id,groupName:groupName,
        DomainName:domainName,DomainID:domainId,IsAnalysis:isAnalysis,
        IsUpload:isUpload,IsThreshold:isThreshold,IsAlarm:isAlarm,IsNotice:isNotice,_method:'put'});
    window.location.reload();
});

